<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<jsp:useBean id="now" class="java.util.Date" scope="page"/>
<!DOCTYPE html>
<html lang="zh">
<html>
<head>
    <title>短信推送</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/formSelects-v4.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <style type="text/css">
        .index-outside {
            min-height: auto !important;
        }

        .index-outside > section .section-main {
            min-width: auto !important;
        }
    </style>
</head>
<body>
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main">
            <!-- 正文请写在这里 -->

            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/member/membermgmt/sendAndSave.do">

                    <input type="hidden" id="behavior" name="behavior" value="${behavior}"/>
                    <input type="hidden" id="id" name="id" value="${targetTemplate.id}"/>
                    <input type="hidden" id="targetId" name="targetId" value="${targetTemplate.targetId}"/>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">推送对象<span class="f-verify-red">*</span></label>
                            <div class="layui-input-block">
                                <select name="memberIds" id="memberIds" lay-filter="memberIds" xm-select="memberIds" xm-select-max="200"
                                        xm-select-show-count="2" xm-select-search="" xm-select-search-type="dl"
                                        <c:if test="${behavior != 'add'}">disabled="disabled"</c:if> >
                                    <c:if test="${behavior != 'add'}">
                                        <c:forEach var="member" items="${members}">
                                            <option value="${member.memberId}" selected="selected" >${member.nickName}</option>
                                        </c:forEach>
                                    </c:if>
                                    <c:if test="${ behavior == 'add'}">
                                        <c:forEach var="member" items="${members}" varStatus="status">
                                            <option value="${member.memberId}" >${status.index +1} ${member.nickName}</option>
                                        </c:forEach>
                                    </c:if>
                                </select>
                            </div>
                            <c:if test="${packageCount.isOpen == 1}">
                                <div class="layui-form-mid layui-word-aux">当前短信剩余<span class="red">${packageCount.totalNum - packageCount.useNum}</span>条</div>
                                <div class="layui-form-mid layui-word-aux"><a href="javascript:forwardCharge();" style="color: #1E9FFF">充值</a></div>
                            </c:if>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">发送时间<c:if test="${behavior != 'add'}"><span class="f-verify-red">*</span></c:if></label>
                            <div class="layui-input-inline">
                                <input class="layui-input" type="text" name="sendTimeString"
                                       value="<fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss" />" placeholder="发送时间">
                                <c:if test="${behavior != 'modifyTime'}">
                                    <div class="layui-form-mid layui-word-aux" style="width: 400px">发送时间小于或等于当前时间,则会立即发送</div>
                                </c:if>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">发送内容<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                      <textarea name="template" placeholder="发送内容" lay-verify="template" class="layui-textarea"
                                style="width: 85%"
                                maxlength="200" onchange="this.value=this.value.substring(0, 300)"
                                onkeydown="this.value=this.value.substring(0, 300)"
                                onkeyup="this.value=this.value.substring(0, 300)"
                                <c:if test="${behavior != 'add'}">disabled="disabled"</c:if>>${targetTemplate.template}</textarea>
                            <div class="layui-form-mid layui-word-aux">最多200字符</div>
                        </div>
                    </div>

                </form>
            </div>
        </div>
    </section>
</div>
<script type="text/javascript" src="${ctxStatic}/common/formSelects-v4.js"></script>
<script type="text/javascript">
    var laytpl = null;
    var laypage = null;
    var element = null;
    var form = null;
    var formSelects = layui.formSelects;

    //超选后的提示
    formSelects.maxTips('memberIds', function(id, vals, val, max){
        //id:   点击select的id
        //vals: 当前select已选中的值
        //val:  当前select点击的值
        //max:  当天多选最大值
        alert("最多选择200个会员！");
    });
    //自定义快捷操作
    formSelects.btns('memberIds', ['skin','remove'], {show: 'icon'});


    $(function () {
        layui.use(['laytpl', 'laypage', 'element', 'form', 'laydate'], function () {
            laytpl = layui.laytpl;
            laypage = layui.laypage;
            element = layui.element;
            form = layui.form;
            var laydate = layui.laydate;

            var sendTime = {
                elem: 'input[name=sendTimeString]',
                type: 'datetime'
            };
            laydate.render(sendTime);
        });
    })



    function doSubmit() {
        var result = true;

        var mobiles = JSON.stringify(layui.formSelects.value('memberIds', 'val'));
        if (mobiles == '[]')
        {
            util.layerMsgError("请选择推送对象")
            result = false;
            return result;
        }

        var template = $('textarea[name=template]').val();
        if (!template || !template.trim()) {
            util.layerMsgError("请填写模板配置")
            result = false;
            return result;
        }

        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: $('#myForm').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    util.layerMsgSuccess("发送成功")
                } else {
                    util.layerMsgError(res.description)
                    result = false;
                }
            },
            url: $("#myForm").attr('action')
        });
        return result;
    }

    function submitQuartz() {
        var result = true;
        var mobiles = JSON.stringify(layui.formSelects.value('memberIds', 'val'));
        if (mobiles == '[]')
        {
            util.layerMsgError("请选择推送对象")
            result = false;
            return result;
        }

        var template = $('textarea[name=template]').val();
        if (!template || !template.trim()) {
            util.layerMsgError("请填写模板配置")
            result = false;
            return result;
        }
        var sendTimeString = $('input[name=sendTimeString]').val();
        if (!sendTimeString) {
            util.layerMsgError("请设置发送时间")
            result = false;
            return result;
        }
        if ($('#behavior').val() == 'modifyTime') {
            var now = new Date();
            if (now.getTime() > new Date(sendTimeString).getTime()) {
                util.layerMsgError("发送时间必须大于当前时间!")
                result = false;
                return result;
            }
        }


        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: $('#myForm').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    util.layerMsgSuccess("发送成功")
                } else {
                    util.layerMsgError(res.description)
                    result = false;
                }
            },
            url: '${ctx}/member/membermgmt/sendQuartzForMembermgmt.do'
        });
        return result;
    }

    function forwardCharge() {
        parent.location.href = "${ctx}/charge/package/smsPackageList.do";
    }




</script>
</body>
</html>
